package encore.portal.config.impl;

import java.io.InputStream;
import java.io.Serializable;
import java.util.Set;

import javax.servlet.ServletContext;

import encore.log.Logger;
import encore.log.LoggerFactory;
import encore.portal.PortalConfigurationException;
import encore.portal.config.PropertyConfigService;
import encore.portal.config.ResourceConfig;
import encore.portal.config.ResourceConfigReader;

/**
 * 该类实现了从XML中获取Portal配置信息
 * @author Plex
 *
 */
public class XMLPropertyConfigService implements Serializable,PropertyConfigService {

	private static final long serialVersionUID = 6792082543931502605L;

	private static final Logger logger=LoggerFactory.getLogger(XMLPropertyConfigService.class);
	
	private ResourceConfig config;
	
	public XMLPropertyConfigService()
	{
		
	}
	
	@Override
	public String getContainerName() {
		return config.getContainerName();
	}

	@Override
	public String getContainerVersion() {
		return config.getContainerVersion();
	}

	@Override
	public String getPortalName() {
		return config.getPortalName();
	}

	@Override
	public String getPortalVersion() {
		return config.getPortalVersion();
	}

	@Override
	public Set<String> getSupportedPortletModes() {
		
		return config.getSupportedPortletModes();
	}

	@Override
	public Set<String> getSupportedWindowStates() {
		// TODO Auto-generated method stub
		return config.getSupportedWindowStates();
	}

	@Override
	public void destroy() throws PortalConfigurationException {
		// TODO Auto-generated method stub
         config=null;
	}

	@Override
	public void init(ServletContext servletContext)
			throws PortalConfigurationException {
		try{
			InputStream inputStream=servletContext.getResourceAsStream(ResourceConfigReader.CONFIG_FILE);
			config=ResourceConfigReader.getFactory().parse(inputStream);
			
			
		}catch(Exception e)
		{
			if(logger.isDebugEnabled())
			{
				logger.debug("Unable to parse resource config "+e.getMessage(), e);
				throw new PortalConfigurationException(e);
			}
			
		}

	}

}
